const fs = require("fs")
const {AVATAR_PATH} = require("../contants/file-path")
const service = require('../service/user.service')
const fileService = require("../service/file.service")

class UserController {
  async create(ctx,next) {
    //获取用户请求传递的参数
    const user = ctx.request.body
    //查询数据
    const result = await service.create(user)
    console.log(result);
    ctx.body = result
    //返回数据
  }

  async avatarInfo(ctx,next){
    // 获取用户的头像
    const {userId} = ctx.request.params;

    let {filename,mimetype} = await fileService.getAvatarByUserId(userId)

    ctx.response.set('content-type',mimetype)
    //2.提供我们的头像信息
    ctx.body = fs.createReadStream(`${AVATAR_PATH}/${filename}`)
  }

}

module.exports = new UserController();